<template>
  <scroll-view
    :scroll-y="true"
    refresher-enabled
    @refresherrefresh="refresherrefresh"
    class="container"
    :show-scrollbar="false"
    :refresher-triggered="triggered"
  >
    <dynamic-card
      class="dynamic-card"
      v-for="dynamic in dynamics"
      :key="dynamic.symbol"
      :username="dynamic.author"
      :content="dynamic.article"
      :image-url="dynamic.background"
      :tomato="dynamic.tomato"
      :head-url="dynamic.head_image"
    />
  </scroll-view>
</template>

<script>
import DynamicCard from "../../../components/DynamicCard";
import { getDynamic } from "../../../api/circle";
export default {
  name: "circle",
  created() {
    this.initDynamics();
  },
  data() {
    return {
      page: 0,
      pageSize: 6,
      triggered: true,
      refresher: false,
      dynamics: [],
    };
  },
  components: {
    DynamicCard,
  },
  methods: {
    initDynamics() {
      return new Promise((resolve) => {
        getDynamic(this.page, this.pageSize).then((res) => {
          const { success, dynamics } = res.data;
          if (success) {
            dynamics.forEach((dynamic) => {
              const { author, article, background, tomato, head_image } = dynamic;
              this.dynamics.push({
                symbol: Symbol(),
                article,
                author,
                background,
                tomato,
                head_image,
              });
            });
            resolve();
          }
        });
      });
    },
    refresherrefresh() {
      this.triggered = true;
      this.initDynamics().then(() => {this.triggered = false;})
    },
    scroll(e) {
      console.log(e.detail.scrollTop);
      if (e.detail.scrollTop <= 0) {
        this.refresher = true;
      } else {
        this.refresher = false;
      }
    },
  },
};
</script>

<style scoped>
.container {
  display: flex;
  align-items: center;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background: #f8f8f8;
  margin: 0 auto;
}
.container > .main > .dynamic-card {
  margin-top: 32rpx;
  width: 100%;
}
</style>